package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.AddressBookService;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class shoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;


    /**
     * 添加购物车
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        //1. 判断一下当前添加的菜品或者套餐在当前登陆用的购物车中是否存在
        //1.1 根据当前登陆用户id和菜品id或者套餐id查询
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        //如果前端传递过来的菜品id不为空，就根据菜品id查询
        wrapper.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
        //如果前端传递过来的套餐id不为空，就根据套餐id查询
        wrapper.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());

        ShoppingCart shoppingCartDo = shoppingCartService.getOne(wrapper);

        if (shoppingCartDo != null) {
            //2. 如果存在(当前商品在购物车中存在)？数量+1
            shoppingCartDo.setNumber(shoppingCartDo.getNumber() + 1);
            shoppingCartService.updateById(shoppingCartDo);
        } else {
            //3. 如果不存在，加入购物车，数量默认为1
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            shoppingCartDo = shoppingCart;
        }

        /*//判断当前添加的菜品\套餐在购物车中是否存在

        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        wrapper.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
        wrapper.eq(shoppingCart.getSetmealId()!=null,ShoppingCart::getSetmealId, shoppingCart.getSetmealId());

        ShoppingCart shoppingCartDo = shoppingCartService.getOne(wrapper);

        if (shoppingCartDo != null) {
            //当前添加在购物车中存在,进行购物车物品数量+1
            shoppingCartDo.setNumber(shoppingCartDo.getNumber() + 1);
            //将当前数据存入数据库中
            shoppingCartService.updateById(shoppingCartDo,);
        } else {
            //在购物车中不存在，将当前数据存入数据库中
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCartService.save(shoppingCart);
            shoppingCartDo = shoppingCart;
        } */
        return R.success(shoppingCartDo);

    }

    /**
     * 购物车列表查询
     *
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {

        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        List<ShoppingCart> cartList = shoppingCartService.list(wrapper);
        return R.success(cartList);
    }

    /**
     * 清空购物车
     */
    @DeleteMapping("/clean")
    public R<String> clean() {
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        shoppingCartService.remove(wrapper);
        return R.success("购物车清除成功");
    }

    /**
     * 删除购物车中的商品
     *
     * @return
     */
    @PostMapping("sub")
    public R<String> delete(@RequestBody ShoppingCart shoppingCart) {
        //查出前端传过来购物车中的id(菜品/套餐)
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
        wrapper.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        ShoppingCart one = shoppingCartService.getOne(wrapper);

        //如果购物车商品中的数量为1，将商品删除
        if (one.getNumber() == 1) {
            shoppingCartService.removeById(one.getId());
        } else {
            //将商品数量减一
            one.setNumber(one.getNumber()-1);
            shoppingCartService.updateById(one);
        }
            return R.success("购物车商品删除成功");

    }
}
